################################################################################
##
## Purpose: This script creates Figure 1, along with SI figure 7
##
## Author: James Bisbee (james.h.bisbee@vanderbilt.edu)
##
## Input Files:
##  - ./data/prepped/finalData.RData: Prepped data from 9_DATA_final_build.R
##
## Output Files:
##  - ./output/figures/MS_figure_1.pdf
##  - ./output/figures/SI_figure_7.pdf
##
##
## See associated log file for compute environment, package versions, 
##  and date of most recent run.
##
################################################################################
rm(list = ls())
gc()
require(tidyverse)
require(ggridges)

set.seed(123)

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

sessionInfo()


load('./data/prepped/finalData.RData')

toplot1 <- utterance_level %>%
  group_by(position,party,gender,name) %>%
  summarise(all = n(),
            interrupted = sum(interrupted)) %>%
  ungroup() %>%
  mutate(propInterrupt = interrupted/all,
         all =  all - interrupted) %>%
  filter(all > 100) %>%
  mutate(id = paste0(str_to_title(name),' (',position,': ',party,'-',gender,')')) %>%
  select(all,interrupted,propInterrupt,id) %>%
  gather(key,value,-propInterrupt,-id)

toplot2 <- utterance_level %>%
  mutate(interruptor = ifelse(interruptor == 1 & interrupted == 1,1,0)) %>%
  group_by(position,party,gender,name) %>%
  summarise(all = n(),
            interruptor = sum(interruptor)) %>%
  ungroup() %>%
  mutate(propInterrupt = interruptor/all,
         all =  all - interruptor) %>%
  mutate(id = paste0(str_to_title(name),' (',position,': ',party,'-',gender,')')) %>%
  select(all,interruptor,propInterrupt,id) %>%
  gather(key,value,-propInterrupt,-id) %>%
  filter(id %in% toplot1$id)

toplot <- toplot1 %>%
  mutate(type = 'Interrupted') %>%
  bind_rows(toplot2 %>%
              mutate(type = 'Interruptor'))

pdf('./output/figures/MS_figure_1.pdf',width = 8,height = 7)
toplot1 %>%
  mutate(key = ifelse(grepl('interrupt',key),'Interruption','All')) %>%
  ggplot(aes(x = value,y = reorder(id,value),group = key,
             color = key,
             fill = key,label = propInterrupt)) + 
  geom_bar(position = 'stack',stat = 'identity') + 
  geom_text(data = toplot1 %>%
              group_by(id,propInterrupt) %>%
              summarise(value = sum(value)) %>%
              mutate(propInterruptedLab = paste0(round(propInterrupt*100),'%'),
                     key = 'All'),
            color = 'black',nudge_x = 80,hjust = 0,
            aes(size = propInterrupt,label = propInterruptedLab),
            alpha = 1) +
  xlab('Total Utterances') + ylab('') + 
  scale_size_continuous(guide = 'none',range = c(2,7)) + 
  scale_x_continuous(expand = c(.15,0)) + 
  scale_fill_manual(name = '',values = c('grey80','grey30')) +
  scale_alpha_manual(name = 'Type',values = c(.5,1)) +
  scale_color_manual(name = '',values = c('white','black')) +
  theme_ridges() + 
  theme(legend.position = 'bottom',
        strip.background = element_rect(fill = 'white',color = 'black',size = .5,linetype = 'solid'),
        axis.text.y = element_text(size = 10,vjust = .5))
dev.off()

# SI
toplot1 <- utterance_level %>%
  group_by(position,party,gender,name) %>%
  summarise(all = n(),
            interrupted = sum(interrupted)) %>%
  ungroup() %>%
  mutate(propInterrupt = interrupted/all,
         all =  all - interrupted) %>%
  filter(all > 30) %>%
  mutate(id = paste0(str_to_title(name),' (',position,': ',party,'-',gender,')')) %>%
  select(all,interrupted,propInterrupt,id) %>%
  gather(key,value,-propInterrupt,-id)

pdf('./output/figures/SI_figure_7.pdf',width = 8,height = 10)
toplot1 %>%
  mutate(key = ifelse(grepl('interrupt',key),'Interruption','All')) %>%
  ggplot(aes(x = value,y = reorder(id,value),group = key,
             color = key,
             fill = key,label = propInterrupt)) + 
  geom_bar(position = 'stack',stat = 'identity') + 
  geom_text(data = toplot1 %>%
              group_by(id,propInterrupt) %>%
              summarise(value = sum(value)) %>%
              mutate(propInterruptedLab = paste0(round(propInterrupt*100),'%'),
                     key = 'All'),
            color = 'black',nudge_x = 80,hjust = 0,
            aes(size = propInterrupt,label = propInterruptedLab),
            alpha = 1) +
  xlab('Total Utterances') + ylab('') + 
  scale_size_continuous(guide = 'none',range = c(2,7)) + 
  scale_x_continuous(expand = c(.15,0)) + 
  scale_fill_manual(name = '',values = c('grey80','grey30')) +
  scale_alpha_manual(name = 'Type',values = c(.5,1)) +
  scale_color_manual(name = '',values = c('white','black')) +
  theme_ridges() + 
  theme(legend.position = 'bottom',
        strip.background = element_rect(fill = 'white',color = 'black',size = .5,linetype = 'solid'),
        axis.text.y = element_text(size = 8,vjust = .5))
dev.off()

# EOF